package com.test.platform.dao;

import com.test.platform.model.DeveloperInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DeveloperInfoMapper {
    @Select("SELECT (@rownum := @rownum + 1) AS displayId, d.* " +
            "FROM developer_info d, (SELECT @rownum := 0) r " +
            "ORDER BY d.id DESC")
    List<DeveloperInfo> findAll();
    
    @Select("SELECT (@rownum := @rownum + 1) AS displayId, d.* " +
            "FROM developer_info d, (SELECT @rownum := 0) r " +
            "WHERE d.owner LIKE CONCAT('%', #{owner}, '%') " +
            "ORDER BY d.id DESC")
    List<DeveloperInfo> findByOwner(@Param("owner") String owner);

    @Select("SELECT (@rownum := @rownum + 1) AS displayId, d.* " +
            "FROM developer_info d, (SELECT @rownum := 0) r " +
            "WHERE d.name LIKE CONCAT('%', #{name}, '%') " +
            "ORDER BY d.id DESC")
    List<DeveloperInfo> findByName(@Param("name") String name);

    // 修改现有的 findByOwnerAndName 方法，添加模糊查询
    @Select("SELECT (@rownum := @rownum + 1) AS displayId, d.* " +
            "FROM developer_info d, (SELECT @rownum := 0) r " +
            "WHERE d.owner LIKE CONCAT('%', #{owner}, '%') " +
            "AND d.name LIKE CONCAT('%', #{name}, '%') " +
            "ORDER BY d.id DESC")
    List<DeveloperInfo> findByOwnerAndName(@Param("owner") String owner, @Param("name") String name);

    @Insert("INSERT INTO developer_info(owner, name, developer, pull_request, title, created_at, updated_at) " +
            "VALUES(#{owner}, #{name}, #{developer}, #{pullRequest}, #{title}, #{createdAt}, #{updatedAt})")
    int insert(DeveloperInfo developerInfo);

    @Update("UPDATE developer_info SET " +
            "owner = #{owner}, " +
            "name = #{name}, " +
            "developer = #{developer}, " +
            "pull_request = #{pullRequest}, " +
            "title = #{title}, " +
            "created_at = #{createdAt}, " +
            "updated_at = #{updatedAt} " +
            "WHERE id = #{id}")
    int update(DeveloperInfo developerInfo);

    @Delete("DELETE FROM developer_info WHERE id = #{id}")
    int deleteById(Integer id);

    @Delete("<script>" +
            "DELETE FROM developer_info WHERE id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int deleteByIds(@Param("ids") List<Integer> ids);

    @Delete("DELETE FROM developer_info WHERE owner = #{owner} AND name = #{name}")
    int deleteByOwnerAndName(@Param("owner") String owner, @Param("name") String name);

    @Delete("DELETE FROM developer_info")
    int deleteAll();

    @Select("SELECT (@rownum := @rownum + 1) AS displayId, d.* " +
            "FROM developer_info d, (SELECT @rownum := 0) r " +
            "WHERE d.id = #{id}")
    DeveloperInfo findById(Integer id);
}